"use strict";
/*
 * @Description: 手机号归属地查询工具，根据手机号码获取归属地信息
 * @Author: 莫若省
 * @date: 2025/9/3
 *
 * 功能说明：
 * 1. 支持通过手机号码查询归属地信息
 * 2. 返回省份、城市和运营商信息
 * 3. 适配新的API响应格式
 */
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __generator = (this && this.__generator) || function (thisArg, body) {
    var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
    return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
    function verb(n) { return function (v) { return step([n, v]); }; }
    function step(op) {
        if (f) throw new TypeError("Generator is already executing.");
        while (g && (g = 0, op[0] && (_ = 0)), _) try {
            if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
            if (y = 0, t) op = [op[0] & 2, t.value];
            switch (op[0]) {
                case 0: case 1: t = op; break;
                case 4: _.label++; return { value: op[1], done: false };
                case 5: _.label++; y = op[1]; op = [0]; continue;
                case 7: op = _.ops.pop(); _.trys.pop(); continue;
                default:
                    if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
                    if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
                    if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
                    if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
                    if (t[2]) _.ops.pop();
                    _.trys.pop(); continue;
            }
            op = body.call(thisArg, _);
        } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
        if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
    }
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.phone_location_tool = void 0;
var zod_1 = require("zod");
var axios_1 = require("axios");
// 手机号归属地查询工具生成器
var phoneLocationToolGenerator = function () { return ({
    name: "手机号归属地查询",
    description: "根据手机号码查询归属地信息，包括省份、城市和运营商",
    prams: {
        phone: zod_1.z.string().regex(/^1[3-9]\d{9}$/).describe("请输入要查询的11位手机号码，例如：13000000000"),
    },
    cb: function (_a) { return __awaiter(void 0, [_a], void 0, function (_b) {
        var apiUrl, response, locationData, error_1, errorMessage;
        var _c;
        var phone = _b.phone;
        return __generator(this, function (_d) {
            switch (_d.label) {
                case 0:
                    _d.trys.push([0, 2, , 3]);
                    apiUrl = "https://uapis.cn/api/v1/misc/phoneinfo?phone=".concat(encodeURIComponent(phone));
                    return [4 /*yield*/, axios_1.default.get(apiUrl, {
                            timeout: 5000, // 5秒超时
                            headers: {
                                'Accept': 'application/json',
                                'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'
                            }
                        })];
                case 1:
                    response = _d.sent();
                    // 检查响应状态和数据格式
                    if (response.status !== 200 || !response.data) {
                        return [2 /*return*/, {
                                content: [{
                                        text: "\u624B\u673A\u53F7\u5F52\u5C5E\u5730API\u8BF7\u6C42\u5931\u8D25\uFF0C\u72B6\u6001\u7801: ".concat(response.status),
                                        type: "text"
                                    }]
                            }];
                    }
                    locationData = response.data;
                    // 格式化响应数据
                    return [2 /*return*/, formatLocationResponse(locationData)];
                case 2:
                    error_1 = _d.sent();
                    console.error("手机号归属地查询错误:", error_1);
                    errorMessage = "手机号归属地查询失败，请稍后重试";
                    if (axios_1.default.isAxiosError(error_1)) {
                        if (error_1.code === 'ECONNABORTED') {
                            errorMessage = "手机号归属地查询请求超时，请检查网络连接";
                        }
                        else if (error_1.response) {
                            errorMessage = ((_c = error_1.response.data) === null || _c === void 0 ? void 0 : _c.msg) || "\u624B\u673A\u53F7\u5F52\u5C5E\u5730API\u670D\u52A1\u5F02\u5E38\uFF0C\u72B6\u6001\u7801: ".concat(error_1.response.status);
                        }
                        else if (error_1.request) {
                            errorMessage = "无法连接到手机号归属地服务，请检查网络连接";
                        }
                    }
                    return [2 /*return*/, {
                            content: [{
                                    text: errorMessage,
                                    type: "text"
                                }]
                        }];
                case 3: return [2 /*return*/];
            }
        });
    }); }
}); };
// 格式化归属地响应
function formatLocationResponse(locationData) {
    // 构建归属地信息文本
    var locationText = "\u624B\u673A\u53F7\u5F52\u5C5E\u5730\u4FE1\u606F\uFF1A\n\n" +
        "\u7701\u4EFD: ".concat(locationData.province, "\n") +
        "\u57CE\u5E02: ".concat(locationData.city, "\n") +
        "\u8FD0\u8425\u5546: ".concat(locationData.sp);
    return {
        content: [
            {
                text: locationText,
                type: "text"
            }
        ]
    };
}
exports.phone_location_tool = phoneLocationToolGenerator;
